
package com.ucpaas.sms.action.componentConf;

import com.jsmsframework.common.constant.SysConstant;
import com.jsmsframework.common.dto.JsmsPage;
import com.jsmsframework.common.util.BeanUtil;
import com.jsmsframework.common.util.StringUtils;
import com.jsmsframework.middleware.dto.ComponentRefMqDTO;
import com.jsmsframework.middleware.entity.JsmsComponentRefMq;
import com.jsmsframework.middleware.service.JsmsComponentRefMqService;
import com.ucpaas.sms.action.BaseAction;
import com.ucpaas.sms.service.componentConf.MqRelationService;
import com.ucpaas.sms.util.CommonUtil;
import com.ucpaas.sms.util.web.StrutsUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * 组件管理-SMSP组件与MQ关联配置 5.17.11版本
 */
@Controller
@Scope("prototype")
@Results({@Result(name = "query", location="/WEB-INF/content/componentConf/mqRelation5.17.11/query.jsp"),
		@Result(name = "edit", location="/WEB-INF/content/componentConf/mqRelation5.17.11/edit.jsp"),
		@Result(name = "createView", location="/WEB-INF/content/componentConf/mqRelation5.17.11/create.jsp")})

public class MqRelation5point17point11Action extends BaseAction {
	
	private static final long serialVersionUID = 284703189102447378L;
	@Autowired
	@Qualifier("mqRelation5point17point11ServiceImpl")
	private MqRelationService mqRelationService;
	@Autowired
	private JsmsComponentRefMqService jsmsComponentRefMqService;
	
	@Action("/componentConf5point17point11/mqRelation/query")
	public String query() {
		Map<String, String> params = StrutsUtils.getFormData();
		JsmsPage<JsmsComponentRefMq> params1= CommonUtil.initJsmsPage(params);
		Map<String, Object> objectMap = new HashMap<>();
		objectMap.putAll(params);
		params1.setParams(objectMap);
		params1.setOrderByClause("update_date desc");
		jpage=jsmsComponentRefMqService.queryList1(params1);
		List<JsmsComponentRefMq> refs=jpage.getData();
		List<ComponentRefMqDTO> refDtos=new ArrayList<>();
		for (JsmsComponentRefMq ref : refs) {
			ComponentRefMqDTO dto=new ComponentRefMqDTO();
			BeanUtil.copyProperties(ref,dto);
			refDtos.add(dto);
		}
		jpage.setData(refDtos);
		//page = mqRelationService.query(params);
		StrutsUtils.setAttribute("component_id_search", params.get("componentId"));
		StrutsUtils.setAttribute("message_type", params.get("messageType"));
		StrutsUtils.setAttribute("mq_id_search", params.get("mqId"));
		StrutsUtils.setAttribute("mode_search", params.get("mode"));
		
		return "query";
	}
	
	@Action("/componentConf5point17point11/mqRelation/queryAllMqQueue")
	public void queryAllMqQueue() {
		Map<String, String> params = StrutsUtils.getFormData();
		dataList = mqRelationService.queryAllMqQueue(params);
		StrutsUtils.renderJson(dataList);
	}
	
	@Action("/componentConf5point17point11/mqRelation/createView")
	public String createView() {
		return "createView";
	}
	
	@Action("/componentConf5point17point11/mqRelation/create")
	public void create() {
		Map<String, String> params = StrutsUtils.getFormData();
		data = mqRelationService.create(params);
		StrutsUtils.renderJson(data);
	}
	
	@Action("/componentConf5point17point11/mqRelation/edit")
	public String editView() {
		String componentId = StrutsUtils.getParameterTrim("componentId");
		String messageType=StrutsUtils.getParameterTrim("messageType");
		String mode=StrutsUtils.getParameterTrim("mode");
		if (NumberUtils.isDigits(componentId) && NumberUtils.isDigits(mode) && StringUtils.isNotBlank(messageType)) {
			data=mqRelationService.editView(Integer.valueOf(componentId),messageType,Integer.valueOf(mode));


		} else {
			data = new HashMap<String, Object>();
		}
		return "createView";
	}



	/**
	 * 根据组件ID+消息类型+mode
	 * 查询所有MQID及
	 * @return
	 */
	@Action("/componentConf5point17point11/mqRelation/searchByCMM")
	public void searchByCompAndMessageAndMode(){
		Map<String, String> params = StrutsUtils.getFormData();
		String componentId = StrutsUtils.getParameterTrim("componentId");
		String messageType=StrutsUtils.getParameterTrim("messageType");
		String mode=StrutsUtils.getParameterTrim("mode");
		if (NumberUtils.isDigits(componentId) && NumberUtils.isDigits(mode) && StringUtils.isNotBlank(messageType)) {
			data=mqRelationService.editView(Integer.valueOf(componentId),messageType,Integer.valueOf(mode));

		}else {
			data = new HashMap<String, Object>();
		}
		StrutsUtils.renderJson(data);
	}

	@Action("/componentConf5point17point11/mqRelation/update")
	public void update() {
		Map<String, String> params = StrutsUtils.getFormData();
		data = mqRelationService.update(params);
		StrutsUtils.renderJson(data);
	}
	
	@Action("/componentConf5point17point11/mqRelation/updateMqGetRate")
	public void updateMqGetRate() {
		Map<String, String> params = StrutsUtils.getFormData();
		data = mqRelationService.updateMqGetRate(params);
		StrutsUtils.renderJson(data);
	}
	
	@Action("/componentConf5point17point11/mqRelation/delete")
	public void delete() {
		String ids = StrutsUtils.getParameterTrim("ids");

		String[] idArray=ids.split(",");
		Map<String, String> params = new HashMap<String, String>();
		for (String s : idArray) {

			params.put("id", s);
			data = mqRelationService.delete(params);
			if(Objects.equals(data.get("result"), SysConstant.FAIL)){
				StrutsUtils.renderJson(data);
				return;
			}
		}


		StrutsUtils.renderJson(data);
	}

	/**
	 * 单个删除MQ关联关系
	 */
	@Action("/componentConf5point17point11/mqRelation/singleDelete")
	public void singleDelete() {
		String id = StrutsUtils.getParameterTrim("id");
		Map<String, Object> result = new HashMap<String, Object>();
		if(NumberUtils.isNumber(id)){
			if(Objects.equals(Integer.valueOf(id),-1)){
				result.put("result",SysConstant.SUCCESS);
				result.put("msg", "新增删除成功");
				data=result;
			}else {
				Map<String, String> params = new HashMap<String, String>();
				params.put("id", id);
				data = mqRelationService.delete(params);
			}


		}else {
			result.put("result",SysConstant.FAIL);
			result.put("msg", "参数非法！");
			data=result;
		}
		StrutsUtils.renderJson(data);
	}
}
